package com.mmp.main.servlet;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mmp.common.bo.Message;
import com.mmp.common.bo.Message.MESSAGE_TYPE;
import com.mmp.common.servlet.BasicServlet;
import com.mmp.main.DB.DBQueryManager;
import com.mmp.main.entity.HospitalDevice;
import com.mmp.main.util.Default;
import com.mmp.main.util.Enums;

public class SearchForDeviceProcessServlet extends BasicServlet {

	private static HashMap<String, String> destinations;

	static {
		destinations = new HashMap<String, String>();
		destinations.put(Default.SEARCH_DEST_UPDATE_DEVICE_PARAM_VALUE, "DeviceInfoUpdateDisplay");
		destinations.put(Default.SEARCH_DEST_SCRAPP_DEVICE_PARAM_VALUE, "DeviceScrappingDisplay");
		destinations.put(Default.SEARCH_DEST_PPM_VISIT_PARAM_VALUE, "PPMVisitDisplay");
		destinations.put(Default.SEARCH_DEST_OPEN_JOB_OREDR_PARAM_VALUE, "JobOrderOpenDisplay");
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String dest = request.getParameter(Default.SEARCH_DEST_PARAM_NAME);

		HospitalDevice searchResult = getDevice(request);

		String msg = null;

		if (searchResult == null) {
			msg = "لم يتم العثور علي أي نتائج موافقة لبحثك";
		} else {
			msg = "عفوا .. لا يمكن إتمام عملية البحث .. ";
			if (Default.SEARCH_DEST_OPEN_JOB_OREDR_PARAM_VALUE.equals(dest)
					&& searchResult.getStatus() != Enums.DEVICE_STATUS.WORK_PROPERLY.getStatus()) {
				if (searchResult.getStatus() == Enums.DEVICE_STATUS.UNDER_MAINTENANCE.getStatus())
					msg += "يوجد أمر عمل مفتوح للجهاز المطلوب";
				else if (searchResult.getStatus() == Enums.DEVICE_STATUS.SCRAPPED.getStatus())
					msg += "الجهاز المطلوب تم تكهينه";
			} else if ((Default.SEARCH_DEST_UPDATE_DEVICE_PARAM_VALUE.equals(dest) || Default.SEARCH_DEST_PPM_VISIT_PARAM_VALUE
					.equals(dest)) && searchResult.getStatus() == Enums.DEVICE_STATUS.SCRAPPED.getStatus()) {
				msg += "الجهاز المطلوب تم تكهينه";
			} else { // search success
				request.setAttribute(Default.DEVICE_INFO_ATTR_NAME, searchResult);
				basicForward(request, response, getSearchDestination(destinations, request));
				return;
			}
		}
		setMessage(request, new Message(msg, MESSAGE_TYPE.ERROR));
		basicForward(request, response, "SearchForDeviceDisplay");

	}

	private HospitalDevice getDevice(HttpServletRequest request) {

		String code = request.getParameter("deviceCode");
		String sn = request.getParameter("deviceSerial");

		/*
		 * if (Default.SEARCH_DEST_OPEN_JOB_OREDR_PARAM_VALUE.equals(dest))
		 * return new DBQueryManager(dbm).getDevice(code, sn,
		 * Enums.DEVICE_STATUS.WORK_PROPERLY, false); else if
		 * ((Default.SEARCH_DEST_UPDATE_DEVICE_PARAM_VALUE.equals(dest)) ||
		 * (Default.SEARCH_DEST_PPM_VISIT_PARAM_VALUE.equals(dest))) return new
		 * DBQueryManager(dbm).getDevice(code, sn, null , true); else if
		 * (Default.SEARCH_DEST_SCRAPP_DEVICE_PARAM_VALUE.equals(dest))
		 */
		return new DBQueryManager(dbm).getDevice(code, sn, null, false);

		// return null;

	}
}
